#include "StdAfx.h"
#include "WaveFormProvider.h"
#include "Randomizer.h"

static Randomizer randomizer;

WaveFormProvider::WaveFormProvider(WaveForm waveForm, SoundBank* soundBank, int frequency, double intensity)
{
	this->waveForm = waveForm;
	this->soundBank = soundBank;
	this->intensity = intensity;
	this->frequency = frequency;
	waveIndex = 0;
}

static void Silence(short* destination, unsigned int length)
{
	for(unsigned int i=0; i<length; i++)
	{
		destination[i] = 0;
	}
}

void WaveFormProvider::GetData(short* destination, unsigned int length)
{
	if(this->frequency==0)
	{
		Silence(destination, length);
		return;
	}

	Sample* sample = soundBank->GetSample(waveForm, frequency);
	if(sample->GetLength()==0)
	{
		Silence(destination, length);
	}
	else
	{
		short* data = sample->GetData();
		unsigned int sampleLen = sample->GetLength();
		for(unsigned int i=0; i<length; i++)
		{
			for(; waveIndex<sampleLen && i<length; waveIndex++, i++)
			{
				destination[i] += data[waveIndex];
			}
			if(i<length)
			{
				waveIndex = 0;
			}
		}
	}
}

void WhiteNoiseProvider::GetData(short* destination, unsigned int length)
{
	for(unsigned int i=0; i<length; i++)
	{
		destination[i] = randomizer.NextShort();
	}
}